package cn.wm.e_01;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class MyServer {
    public static void main(String[] args) {
        EventLoopGroup boss = new NioEventLoopGroup(1);//事件循环组 异步IO线程组 接受链接 转给WORKER
        EventLoopGroup worker = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)//通过反射创建
                    .option(ChannelOption.SO_BACKLOG,1024)//设置服务器可连接的线程数
                    .childHandler(new MyInitializer());//自己定义的初始化器



            ChannelFuture sync = bootstrap.bind(9999).sync();//同步监听哪个端口
            System.out.println("start...");
            sync.channel().closeFuture();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("bye...");
                boss.shutdownGracefully();
                worker.shutdownGracefully();
            }
        }));

    }
}
